//
//  dadaInfoMakeVC.m
//  qqqqqq
//
//  Created by iOSer on 2016/12/22.
//  Copyright © 2016年 iOSer. All rights reserved.
//

#import "dadaInfoMakeVC.h"
#import "DadaAnimationVC.h"
@interface dadaInfoMakeVC ()<UITextFieldDelegate>
//车牌号
@property(nonatomic,strong) UILabel *chePaiLab;
@property(nonatomic,strong) UITextField *chePaiTextField;
//手机号
@property(nonatomic,strong) UILabel *phoneLab;
@property(nonatomic,strong) UITextField *phoneTextField;
//今日口令
@property(nonatomic,strong) UILabel *dayPassLab;
@property(nonatomic,strong) UITextField *dayPassField;
//车图片选择
@property(nonatomic,strong) UIButton *yellowCar;
@property(nonatomic,strong) UIButton *otherCar;
@property(nonatomic,strong) UIButton *goBtn;
@property(nonatomic,copy) NSString *carImgName;
@end

@implementation dadaInfoMakeVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:HEXColor(0xffffff, 1)];
    [self.view addGestureRecognizer: [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(ViewTaped)]];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.navigationItem.title = @"信息设置";
    [self otherCarBtnClick];

}

-(void)viewWillAppear:(BOOL)animated{
    NSDictionary *info = [[GGDataBase shareDataBase] getCacheForKey:DADA_INFO_CACHE_KEY];
    NSString *phoneStr = info[@"phone"];
    if (phoneStr.length!=0) {
        self.phoneTextField.text = phoneStr;
    }
    NSString *chepaihaoStr = info[@"chepaihao"];
    if (chepaihaoStr.length!=0) {
        self.chePaiTextField.text = chepaihaoStr;
    }
    [self setupUI];
}
-(void)setupUI{
    [self.view addSubview:self.chePaiLab];
    [self.view addSubview:self.chePaiTextField];
    [self.view addSubview:self.phoneTextField];
    [self.view addSubview:self.phoneLab];

    [self.view addSubview:self.dayPassLab];
    [self.view addSubview:self.dayPassField];
    [self.view addSubview:self.yellowCar];
    [self.view addSubview:self.otherCar];
    [self.view addSubview:self.goBtn];
    
    [_chePaiTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom).offset(GGRealValue(60));
//        make.left.equalTo(self.view).offset(GGRealValue(100));
        make.centerX.equalTo(self.view);
        make.height.equalTo(GGRealValue(30));
        make.width.equalTo(GGRealValue(150));
    }];
    [_chePaiLab makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_chePaiTextField.mas_left).offset(-GGRealValue(10));
        make.centerY.equalTo(_chePaiTextField);
    }];
    [_phoneTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_chePaiTextField.mas_bottom).offset(GGRealValue(60));
        make.centerX.equalTo(self.view);
        make.height.equalTo(GGRealValue(30));
        make.width.equalTo(GGRealValue(150));
    }];
    [_phoneLab makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_phoneTextField.mas_left).offset(-GGRealValue(10));
        make.centerY.equalTo(_phoneTextField);
    }];
    [_dayPassField makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_phoneTextField.mas_bottom).offset(GGRealValue(60));
        make.centerX.equalTo(self.view);
        make.height.equalTo(GGRealValue(30));
        make.width.equalTo(GGRealValue(150));
    }];
    [_dayPassLab makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_dayPassField.mas_left).offset(-GGRealValue(10));
        make.centerY.equalTo(_dayPassField);
    }];
    [_yellowCar makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(GGRealValue(50));
        make.top.equalTo(_dayPassLab.mas_bottom).offset(60);
        make.size.equalTo(CGSizeMake(GGRealValue(100), GGRealValue(30)));
    }];
    [_otherCar makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view).offset(-GGRealValue(50));
        make.top.equalTo(_dayPassLab.mas_bottom).offset(60);
        make.size.equalTo(CGSizeMake(GGRealValue(100), GGRealValue(30)));
    }];
    [_goBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_otherCar.mas_bottom).offset(GGRealValue(40));
        make.centerX.equalTo(self.view);
        make.size.equalTo(CGSizeMake(GGRealValue(150), GGRealValue(50)));
    }];
    
}


- (void)ViewTaped{
    [self.view endEditing:YES];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField

{
    if (textField != self.chePaiTextField) {
        [self animateTextField: textField up: YES];
    }
    
    
}





- (void)textFieldDidEndEditing:(UITextField *)textField

{
    if (textField != self.chePaiTextField) {
        [self animateTextField: textField up: NO];
    }
    
    
}



- (void) animateTextField: (UITextField*) textField up: (BOOL) up

{
    
    const int movementDistance = 80; // tweak as needed
    
    const float movementDuration = 0.3f; // tweak as needed
    
    
    
    int movement = (up ? -movementDistance : movementDistance);
    
    
    
    [UIView beginAnimations: @"anim" context: nil];
    
    [UIView setAnimationBeginsFromCurrentState: YES];
    
    [UIView setAnimationDuration: movementDuration];
    
    self.view.frame = CGRectOffset(self.view.frame, 0, movement);
    
    [UIView commitAnimations];
    
}
-(void)gobtnClick{
    if (self.phoneTextField.text.length !=0 && self.dayPassField.text.length != 0) {
        
        if ([[DefaultTools sharedInstance] validatePhoneNumber:self.phoneTextField.text] == YES) {
            NSDictionary *dic = @{
                                  @"phone":self.phoneTextField.text,
                                  @"chepaihao":self.chePaiTextField.text,
                                  @"kouling":self.dayPassField.text,
                                  @"carImg":self.carImgName
                                  };
            [[GGDataBase shareDataBase] setCacheWithData:dic Key:DADA_INFO_CACHE_KEY];
            DadaAnimationVC *dadaVC = [DadaAnimationVC new];
            [self.navigationController pushViewController:dadaVC animated:YES];
        }else{
            [[GGShowHUDTool sharedInstance] showNoHudWith:@"输入正确手机号！！！！"];
        }
    }else{
        [[GGShowHUDTool sharedInstance] showInfoHudWith:@"手机号和每日口令必须有！"];
    }

}
-(void)yellowCarBtnClick{
    self.yellowCar.selected = YES;
    self.otherCar.selected = NO;
    self.carImgName = @"dada_car";
}
-(void)otherCarBtnClick{
    self.yellowCar.selected = NO;
    self.otherCar.selected = YES;
    self.carImgName = @"dada_redBag";
}
-(UITextField *)phoneTextField{
    if ((!_phoneTextField)) {
        _phoneTextField = [UITextField new];
        _phoneTextField.borderStyle = UITextBorderStyleRoundedRect;
        _phoneTextField.backgroundColor = [UIColor clearColor];
        _phoneTextField.textColor = HEXColor(0x050505, 1);
        _phoneTextField.keyboardType = UIKeyboardTypePhonePad;
        _phoneTextField.clearButtonMode = UITextFieldViewModeNever;
        _phoneTextField.placeholder = @"请输入手机号";
        //        _passTextField.text = @"florence1122";
        _phoneTextField.delegate = self;
        _phoneTextField.tintColor = HEXColor(0x050505, 1);
        _phoneTextField.font = [UIFont systemFontOfSize:GGRealValue(18)];
        //        _phoneTextField.secureTextEntry =YES;
    }
    return _phoneTextField;
}
-(UITextField *)chePaiTextField{
    if ((!_chePaiTextField)) {
        _chePaiTextField = [UITextField new];
        _chePaiTextField.borderStyle = UITextBorderStyleRoundedRect;
        _chePaiTextField.backgroundColor = [UIColor clearColor];
        _chePaiTextField.textColor = HEXColor(0x050505, 1);
        _chePaiTextField.keyboardType = UIKeyboardTypeDefault;
        _chePaiTextField.clearButtonMode = UITextFieldViewModeNever;
        _chePaiTextField.placeholder = @"请输入车牌号";
        _chePaiTextField.text = @"京";
        _chePaiTextField.delegate = self;
        _chePaiTextField.tintColor = HEXColor(0x050505, 1);
        _chePaiTextField.font = [UIFont systemFontOfSize:GGRealValue(18)];
        //        _phoneTextField.secureTextEntry =YES;
    }
    return _chePaiTextField;
}

-(UITextField *)dayPassField{
    if ((!_dayPassField)) {
        _dayPassField = [UITextField new];
        _dayPassField.borderStyle = UITextBorderStyleRoundedRect;
        _dayPassField.backgroundColor = [UIColor clearColor];
        _dayPassField.textColor = HEXColor(0x050505, 1);
        _dayPassField.keyboardType = UIKeyboardTypeDefault;
        _dayPassField.clearButtonMode = UITextFieldViewModeNever;
        //        _passTextField.text = @"florence1122";
        _dayPassField.delegate = self;
        _dayPassField.tintColor = HEXColor(0x050505, 1);
        _dayPassField.font = [UIFont systemFontOfSize:GGRealValue(18)];
        _dayPassField.placeholder = @"今日口令";
    }
    return _dayPassField;
}
-(UILabel *)chePaiLab{
    if (!_chePaiLab) {
        _chePaiLab = [[UILabel alloc] init];
        _chePaiLab.font = [UIFont fontWithName:@"MicrosoftYaHei" size:GGRealValue(18)];
        _chePaiLab.textColor = HEXColor(0x050505, 1);
        _chePaiLab.text = @"车牌号:";
        _chePaiLab.textAlignment = NSTextAlignmentRight;
        [_chePaiLab sizeToFit];
    }
    return _chePaiLab;
}
-(UILabel *)phoneLab{
    if (!_phoneLab) {
        _phoneLab = [[UILabel alloc] init];
        _phoneLab.font = [UIFont fontWithName:@"MicrosoftYaHei" size:GGRealValue(18)];
        _phoneLab.textColor = HEXColor(0x050505, 1);
        _phoneLab.text = @"手机号:";
        _phoneLab.textAlignment = NSTextAlignmentRight;
        [_phoneLab sizeToFit];
    }
    return _phoneLab;
}

-(UILabel *)dayPassLab{
    if (!_dayPassLab) {
        _dayPassLab = [[UILabel alloc] init];
        _dayPassLab.font = [UIFont fontWithName:@"MicrosoftYaHei" size:GGRealValue(18)];
        _dayPassLab.textColor = HEXColor(0x050505, 1);
        _dayPassLab.text = @"口令:";
        _dayPassLab.textAlignment = NSTextAlignmentRight;
        [_dayPassLab sizeToFit];
    }
    return _dayPassLab;
}
-(UIButton *)goBtn{
    if (!_goBtn) {
        _goBtn = [UIButton new];
        [_goBtn setTitle:@"GO!" forState:UIControlStateNormal];
        [_goBtn setTintColor:HEXColor(0xffffff, 1)];
        [_goBtn setBackgroundColor:HEXColor(0x050505, 1)];
        [_goBtn addTarget:self action:@selector(gobtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goBtn;
}
-(UIButton *)yellowCar{
    if (!_yellowCar) {
        _yellowCar = [UIButton new];
        [_yellowCar setImage:[UIImage imageNamed:@"dada_danxuanNomal"] forState:UIControlStateNormal];
        [_yellowCar setImage:[UIImage imageNamed:@"dada_danxuancLick"] forState:UIControlStateSelected];
        [_yellowCar setTitleColor:HEXColor(0x050505, 1) forState:UIControlStateNormal];
        [_yellowCar setTitleColor:HEXColor(0x050505, 1) forState:UIControlStateSelected];
        [_yellowCar setTitle:@"小黄车" forState:UIControlStateNormal];
        [_yellowCar setTitle:@"小黄车" forState:UIControlStateSelected];
        [_yellowCar addTarget:self action:@selector(yellowCarBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yellowCar;
}
-(UIButton *)otherCar{
    if (!_otherCar) {
        _otherCar = [UIButton new];
        [_otherCar setImage:[UIImage imageNamed:@"dada_danxuanNomal"] forState:UIControlStateNormal];
        [_otherCar setImage:[UIImage imageNamed:@"dada_danxuancLick"] forState:UIControlStateSelected];
        [_otherCar setTitleColor:HEXColor(0x050505, 1) forState:UIControlStateNormal];
        [_otherCar setTitleColor:HEXColor(0x050505, 1) forState:UIControlStateSelected];
        [_otherCar setTitle:@"其他" forState:UIControlStateNormal];
        [_otherCar setTitle:@"其他" forState:UIControlStateSelected];
        [_otherCar addTarget:self action:@selector(otherCarBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _otherCar;
}
-(void)setCarImgName:(NSString *)carImgName{
    _carImgName = carImgName;
}
@end
